/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge.major;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import ru.jSkyRoom.bridge.IteratorBridge;
import ru.jSkyRoom.bridge.LeveledEntryBridge;
import ru.jSkyRoom.finder.FinderByFormId;
import ru.jSkyRoom.interfaces.JSkyLeveledEntry;
import ru.jSkyRoom.interfaces.Leveled;
import ru.jSkyRoom.interfaces.Major;
import skyproc.LeveledEntry;
import skyproc.LeveledRecord;
import skyproc.MajorRecord;
import skyproc.exceptions.BadParameter;

/**
 *
 * @author Rakev
 */
public class LeveledBridge<E extends LeveledRecord> extends MajorBridge<E> implements Leveled {

    public LeveledBridge(E adapting) {
        super(adapting);
    }

    @Override
    public Collection<JSkyLeveledEntry> getEntries() {
        Collection<LeveledEntry> adaptingEntrys = getAdapting().getEntries();
        Collection<LeveledEntryBridge> entryBridges = getBridgeFactory().createBridges(adaptingEntrys, LeveledEntryBridge.class);
        
        return Collections.<JSkyLeveledEntry>unmodifiableCollection(entryBridges);
    }

    @Override
    public void addEntry(JSkyLeveledEntry entry) {
        if (entry instanceof LeveledEntryBridge) {
            LeveledEntry rawEntry = ((LeveledEntryBridge) entry).getAdapting();
            getAdapting().addEntry(rawEntry);
        }
        throw new IllegalArgumentException("Only bridge supported.");
    }

    @Override
    public void addEntry(Major major, int level, int count) {
        getAdapting().addEntry(major.getForm(), level, count);
    }

    @Override
    public void removeEntry(JSkyLeveledEntry entry) {
        if (entry instanceof LeveledEntryBridge) {
            LeveledEntry rawEntry = ((LeveledEntryBridge) entry).getAdapting();
            getAdapting().getEntries().remove(rawEntry);
        }
        throw new IllegalArgumentException("Only bridge supported.");
    }

    @Override
    public Iterator<JSkyLeveledEntry> iterator() {
        Iterator<LeveledEntry> rawIterator = getAdapting().iterator();
        return (Iterator) new IteratorBridge<LeveledEntry, LeveledEntryBridge>(rawIterator);
    }

    @Override
    public int getChanceNone() {
        return getAdapting().getChanceNone();
    }

    @Override
    public void setChanceNone(int in) throws BadParameter {
        getAdapting().setChanceNone(in);
    }

    @Override
    public Double getChanceNonePct() {
        return getAdapting().getChanceNonePct();
    }

    @Override
    public boolean getFlag(LeveledRecord.LVLFlag flag) {
        return getAdapting().get(flag);
    }

    @Override
    public void setFlag(LeveledRecord.LVLFlag flag) {
        getAdapting().set(flag, true);
    }

    @Override
    public void unsetFlag(LeveledRecord.LVLFlag flag) {
        getAdapting().set(flag, false);
    }

}
